using System;
using System.Windows;
using System.Configuration;
using System.Windows.Threading;
using System.Diagnostics;
using Yorrick.FlickrMetadataSynchr.Log;
using System.Reflection;

namespace Yorrick.FlickrMetadataSynchr
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : System.Windows.Application
    {
        #region Constructor
        /// <summary>
        /// Constructs an instance.
        /// </summary>
        public App()
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            ActivityCollector.CreateSingleton(Dispatcher);

            Trace.Listeners.Add(new ActivityTraceListener(ActivityCollector.Singleton));

            TraceAppStartInformation();
        }
        #endregion

        #region Event handlers
        /// <summary>
        /// Event handler for unhandled exceptions..
        /// </summary>
        /// <param name="sender">Not used.</param>
        /// <param name="e">Information about the exception.</param>
        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Trace.TraceError("Unhandled exception occurred. The application will shut down. Exception details follow:");
            Trace.TraceError(e.ExceptionObject.ToString());
            Trace.Flush();
        }
        #endregion

        #region Private methods
        /// <summary>
        /// Writes application information like version number to the trace listeners at application start.
        /// </summary>
        private static void TraceAppStartInformation()
        {
            Version appVersion = Assembly.GetExecutingAssembly().GetName().Version;

            Version flickrNetVersion = Assembly.GetAssembly(typeof(FlickrNet.Flickr)).GetName().Version;

            Trace.TraceInformation("Starting FlickrMetadataSynchr version {0} using FlickrNet {1}.",  appVersion.ToString(), flickrNetVersion.ToString());
        }
        #endregion
    }
}